package com.zhongwang.mms.module.material.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.TDept;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.basic.service.MMaterialDictionaryService;
import com.zhongwang.mms.module.basic.service.TDeptService;
import com.zhongwang.mms.module.material.dao.CompositeOutcomeDao;
import com.zhongwang.mms.module.material.dao.MOrderDao;
import com.zhongwang.mms.module.material.dao.MOrderDetailDao;
import com.zhongwang.mms.module.material.model.CompositeOutcomeModel;
import com.zhongwang.mms.module.material.model.MOrderInsertModel;
import com.zhongwang.mms.module.material.service.MOrderService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 原料出入库 控制层
 * @Author: China.wsg
 * @Date: 2020/04/01 11:23
 */
@Controller
@RequestMapping("/material/order")
public class MOrderController extends BaseController {

    @Autowired
    private MOrderService mOrderService;
    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;
    @Autowired
    private MMaterialDictionaryService mMaterialDictionaryService;
    @Autowired
    private MOrderDao mOrderDao;
    @Autowired
    private MOrderDetailDao mOrderDetailDao;
    @Autowired
    private CompositeOutcomeDao compositeOutcomeDao;
    @Autowired
    private TDeptService deptService;

    /**
     * 原料入库 页面
     *
     * @return 页面路径
     */
    @GetMapping("/inCreate")
    @RequiresPermissions({"matwar:matinwar:view"})
    public String inCreatePage(Model model) {
        // 获取登入用户实体
        model.addAttribute("odrMakePerson", ShiroUtils.getUser().getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原料库"));
        return "material/matWarManage/InManage/create";
    }

    /**
     * 创建原材料入库单
     *
     * @param mOrderInsertModel
     * @return
     */
    @ResponseBody
    @PostMapping("/inCreate")
    @RequiresPermissions({"matwar:matinwar:edit"})
    public JsonResult inCreate(@RequestBody MOrderInsertModel mOrderInsertModel) {

        JsonResult jsonResult = new JsonResult();
        String msg = mOrderService.saveInOrderModel(mOrderInsertModel);
        if (msg.equals("单据保存成功！")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 原料出库 页面
     *
     * @return 页面路径
     */
    @GetMapping("/outCreate")
    @RequiresPermissions({"matwar:matoutwar:view"})
    public String outCreatePage(Model model) {
        // 获取登入用户实体
        model.addAttribute("matType", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialTypeList());
        model.addAttribute("matQuality", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialQualityList());
        model.addAttribute("matSpec", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialSpecList());
        model.addAttribute("odrMakePerson", ShiroUtils.getUser().getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("dept", deptService.findDeptInfo(new TDept()));
        model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原料库"));
        return "material/matWarManage/OutManage/create";
    }

    /**
     * 创建原材料出库单
     *
     * @param mOrderInsertModel
     * @return
     */
    @ResponseBody
    @PostMapping("/outCreate")
    @RequiresPermissions({"matwar:matoutwar:edit"})
    public JsonResult outCreate(@RequestBody MOrderInsertModel mOrderInsertModel) {

        JsonResult jsonResult = new JsonResult();
        String msg = mOrderService.saveOutOrderModel(mOrderInsertModel);
        if (!msg.contains("保存失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 冲单页面 页面
     *
     * @return 页面路径
     */
    @GetMapping("/redOrderDetail")
    @RequiresPermissions({"matinv:matreverse:view"})
    public String redOrderDetail() {
        return "material/matWarManage/RedOrderDetail/list";
    }

    /**
     * 获取主单信息
     *
     * @param odrId 主单Id
     * @return
     */
    @ResponseBody
    @GetMapping("/orderMaster")
    public JsonResult getOrderMaster(Integer odrId) {

        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(mOrderDao.selectByPrimaryKey(odrId));
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 获取单据明细（根据主单Id）
     *
     * @param odrId 主单Id
     * @return
     */
    @ResponseBody
    @GetMapping("/orderDetail")
    public JsonResult getOrderDetailById(Integer odrId) {

        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(mOrderDetailDao.findOrderDetailByPrimaryKey(odrId));
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 获取单据明细（根据检索条件）
     *
     * @param compositeOutcomeModel 检索条件
     * @return
     */
    @ResponseBody
    @PostMapping("/orderDetailInfo")
    public JsonResult getOrderDetailByQuery(CompositeOutcomeModel compositeOutcomeModel) {
        startPage();
        List<CompositeOutcomeModel> list = compositeOutcomeDao.findCompositeOutcome(compositeOutcomeModel);
        return jsonResult(list);
    }

    /**
     * 明细红冲（单条红冲）
     *
     * @param odaId 明细Id
     * @return
     */
    @ResponseBody
    @PostMapping("/redDetailSingle")
    @RequiresPermissions({"matinv:matreverse:edit"})
    public JsonResult redDetailSingle(Integer odaId) {

        JsonResult jsonResult = new JsonResult();
        String msg = mOrderService.redDetailSingle(odaId);
        if (!msg.contains("红冲失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 明细红冲（批量红冲）
     *
     * @param arr 明细Id集合
     * @return
     */
    @ResponseBody
    @PostMapping("/redDetailTogether")
    @RequiresPermissions({"matinv:matreverse:edit"})
    public JsonResult redDetailTogether(@RequestBody List<Integer> arr) {

        JsonResult jsonResult = new JsonResult();
        String msg = mOrderService.redDetailTogether(arr);
        if (!msg.contains("红冲失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 入库/出库 查看页面
     *
     * @param odrId 主单Id
     * @param model
     * @return 页面路径
     */
    @GetMapping("/view")
    @RequiresPermissions(value = {"matwar:matinwar:view", "matwar:matoutwar:view", "matwar:matwarquery:view"}, logical = Logical.OR)
    public String view(@RequestParam(value = "odrId", required = false) Integer odrId, @RequestParam(value = "odrType", required = false) Integer odrType, Model model) {

        String prefix = "";
        if (odrType == 1 || odrType == 2 || odrType == 6) {
            // 入库单
            prefix = "material/matWarManage/InManage/view";
        } else if (odrType == 4 || odrType == 5) {
            // 出库单
            prefix = "material/matWarManage/OutManage/view";
        }
        model.addAttribute("dept", deptService.findDeptInfo(new TDept()));
        model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原料库"));
        model.addAttribute("order", mOrderDao.selectByPrimaryKey(odrId));
        return prefix;
    }
}
